{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import dlib\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "predictor_model = 'shape_predictor_68_face_landmarks.dat'\n",
    "detector = dlib.get_frontal_face_detector()# dlib人脸检测器\n",
    "predictor = dlib.shape_predictor(predictor_model)\n",
    "\n",
    "# cv2读取图像\n",
    "test_img_path = \"input/Messi.jpg\"\n",
    "output_pos_info = \"output_pos_info/Messi.txt\"\n",
    "img = cv2.imread(test_img_path)\n",
    "file_handle = open(output_pos_info, 'a')\n",
    "# 取灰度\n",
    "img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n",
    "\n",
    "# 人脸数rects\n",
    "rects = detector(img_gray, 0)\n",
    "\n",
    "\n",
    "for i in range(len(rects)):\n",
    "    landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])\n",
    "    for idx, point in enumerate(landmarks):\n",
    "        # 68点的坐标\n",
    "        pos = (point[0, 0], point[0, 1])\n",
    "        print(idx+1, pos)\n",
    "        pos_info = str(point[0, 0]) + ' ' + str(point[0, 1]) + '\\n'\n",
    "        file_handle.write(pos_info)\n",
    "        # 利用cv2.circle给每个特征点画一个圈，共68个\n",
    "        cv2.circle(img, pos, 3, color=(0, 255, 0))\n",
    "        # 利用cv2.putText输出1-68\n",
    "        #font = cv2.FONT_HERSHEY_SIMPLEX\n",
    "        #cv2.putText(img, str(idx+1), pos, font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)\n",
    "\n",
    "file_handle.close()\n",
    "cv2.imwrite(\"output/result_Messi.png\", img)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rectangles[[(137, 161) (352, 376)]]\n"
     ]
    }
   ],
   "source": [
    "print(rects)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<dlib.full_object_detection object at 0x1082158b8>\n"
     ]
    }
   ],
   "source": [
    "print(predictor(img,rects[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "points[(151, 228), (153, 255), (157, 283), (161, 309), (168, 333), (181, 356), (198, 375), (218, 391), (245, 396), (273, 393), (293, 378), (311, 358), (325, 336), (334, 312), (340, 287), (346, 261), (349, 235), (169, 211), (183, 196), (203, 191), (223, 193), (243, 200), (270, 203), (289, 198), (308, 197), (326, 205), (335, 221), (254, 217), (253, 236), (252, 255), (250, 275), (230, 286), (240, 290), (250, 293), (260, 291), (270, 288), (194, 220), (204, 213), (217, 214), (229, 223), (217, 224), (204, 224), (278, 225), (290, 218), (303, 219), (312, 226), (302, 230), (290, 229), (216, 324), (227, 318), (240, 314), (250, 318), (261, 315), (274, 321), (285, 328), (274, 336), (261, 341), (249, 341), (238, 340), (226, 334), (221, 325), (239, 324), (250, 327), (261, 325), (280, 328), (261, 326), (249, 328), (239, 325)]\n"
     ]
    }
   ],
   "source": [
    "print(predictor(img, rects[0]).parts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
